package com.scau.yangsuiyu.estore.controller.myComponent;

import com.scau.yangsuiyu.estore.controller.buyer.CartPane;
import com.scau.yangsuiyu.estore.dababases.dao.CartDao;
import com.scau.yangsuiyu.estore.dababases.dao.ItemDao;
import com.scau.yangsuiyu.estore.dababases.entity.Goods;
import com.scau.yangsuiyu.estore.dababases.entity.Item;
import com.scau.yangsuiyu.estore.util.ImageViewUtil;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 购物车中商品的HBox
 * @date 2019-06-21 14:03
 * @email suiyu_yang@163.com
 */
public class CartGoodsHBox extends HBox implements Initializable {
    private Goods goods;
    private Item item;
    private CartPane cartPane;
    @FXML
    private TextField goodsNumTF;

    @FXML
    private CheckBox goodsSelectedCB;

    @FXML
    private ImageView goodsPhoto;

    @FXML
    private Label goodsNameLabel;

    @FXML
    private Label priceLabel;

    @FXML
    private Label promotionalPriceLabel;

    @FXML
    private Label paymentPriceLabel;

    public CartGoodsHBox(CartPane cartPane, Item item) {
        this.item = item;
        this.goods = item.getGoods();
        this.cartPane = cartPane;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/myComponent/CartGoodsHBox.fxml"));
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        setItemToUI();
        String regex = "^[0-9]*$";
        goodsNumTF.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (!newValue.matches(regex)) {
                    goodsNumTF.setText(oldValue);
                }
                if (newValue.equals("")) {
                    goodsNumTF.setText("1");
                }
            }
        });
    }

    @FXML
    private void plus() {
        int num = Integer.parseInt(goodsNumTF.getText());
        num++;
        if (num < goods.getInventory()) {
            goodsNumTF.setText(num + "");
            item.setAmount(num);
        } else {
            item.setAmount(goods.getInventory());
            goodsNumTF.setText(goods.getInventory() + "");
        }

        int newAmount = item.getAmount();
        item.setPaymentPrice(item.getGoods().getPromotionPrice() * newAmount);
        item.setTotalPrice(item.getGoods().getPrice() * newAmount);

        new ItemDao().update(item);
        setItemToUI();
    }

    @FXML
    private void minus() {
        int num = Integer.parseInt(goodsNumTF.getText());
        num--;
        if (num > 0) {
            goodsNumTF.setText(num + "");
            item.setAmount(num);
        } else {
            item.setAmount(1);
            goodsNumTF.setText("1");
        }

        int newAmount = item.getAmount();
        item.setPaymentPrice(item.getGoods().getPromotionPrice() * newAmount);
        item.setTotalPrice(item.getGoods().getPrice() * newAmount);

        new ItemDao().update(item);
        setItemToUI();
    }

    @FXML
    private void delete() {
        new ItemDao().delete(item);
        cartPane.updatePages();
    }

    private void setItemToUI() {
        int amount = Integer.parseInt(goodsNumTF.getText());
        ImageViewUtil.loadImage(goodsPhoto, goods.getGoodsPhotoList().get(0).getPhotoUrl());
        goodsNameLabel.setText(goods.getGoodsName());
        priceLabel.setText("¥" + goods.getPrice());
        promotionalPriceLabel.setText("¥" + goods.getPromotionPrice());
        paymentPriceLabel.setText("¥" + goods.getPromotionPrice() * amount);
        goodsNumTF.setText(item.getAmount() + "");
        paymentPriceLabel.setText("¥" + item.getPaymentPrice());
    }

    public boolean isItemSelected() {
        return goodsSelectedCB.isSelected();
    }

    public Item getItem() {
        return this.item;
    }

    public void setSelectGoods(boolean selected) {
        this.goodsSelectedCB.setSelected(selected);
    }

}
